package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.HdWallet;
import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.qrcodeview.QRCodeUtil;
import com.chainyoung.component_tmperson.zxing.WriterException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看私钥
 */
public class HdWalletPrivateKeyActivity extends BaseActivity {


    @BindView(R2.id.iv_code)
    ImageView ivCode;
    @BindView(R2.id.tv_private_key)
    TextView tvPrivateKey;


    private HdWalletCache hdWallet;

    //剪切板管理工具类
    private ClipboardManager mClipboardManager;
    //剪切板Data对象
    private ClipData mClipData;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        setCurrentTitle("备份私钥");
        hdWallet = (HdWalletCache) getIntent().getSerializableExtra("hdwallet");
        if (hdWallet != null) {
            generateQrcode();
        }
        mClipboardManager = (ClipboardManager) mContext.getSystemService(mContext.CLIPBOARD_SERVICE);
    }

    @Override
    protected void initData() {

    }

    private void generateQrcode() {
        tvPrivateKey.setText(hdWallet.getPrivateKey());
        try {
            String codeText = hdWallet.getPrivateKey();
            BitmapDrawable bd = (BitmapDrawable) ContextCompat.getDrawable(mContext, R.mipmap.icon_eth);
            Bitmap bitmap = bd.getBitmap();
            int width = ConvertUtils.dp2px(mContext, 200);
            Bitmap bm = QRCodeUtil.createQRCode(codeText, bitmap, width, width);
            ivCode.setImageBitmap(bm);
            ivCode.setScaleType(ImageView.ScaleType.FIT_XY);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayoutId() {

        return R.layout.tmperson_activity_hdwallet_private_key;

    }


    @OnClick({R2.id.tv_private_key})
    void onClick(View view) {
        if (view.getId() == R.id.tv_private_key) {
            String text = tvPrivateKey.getText().toString().trim();
            //创建一个新的文本clip对象
            mClipData = ClipData.newPlainText("block", text);
            //把clip对象放在剪贴板中
            mClipboardManager.setPrimaryClip(mClipData);
            ToastUtil.showToast(getString(R.string.tmperson_copy_success));
        }
    }



}
